﻿using jbt.CodeCandy.AsTrEAM.Contracts;
using System;
using System.Threading.Tasks;

namespace jbt.CodeCandy.AsTrEAM.StreamManager
{
    /// <summary>
    /// Implements an <see cref="IStreamManager"/> object what has the implementation injected at runtime.
    /// </summary>
    public sealed class GenericStreamManager : IStreamManager
    {
        /// <summary>
        /// Gets or sets the implementation of <see cref="IStreamManager.Create"/> method.
        /// </summary>
        public Func<StreamData, Task<NewStreamResult>> Create
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the implementation of <see cref="IStreamManager.Get"/> method.
        /// </summary>
        public Func<string, Task<StreamData>> Get
        {
            get;
            set;
        }

        #region IStreamManager Members

        Task<NewStreamResult> IStreamManager.Create(StreamData fileData)
        {
            if (Create == null)
            {
                throw new NotImplementedException("Provide an implementation in GenericStreamManager.CreateFile method.");
            }

            var newFileTask = Task.Run(() => Create(fileData));

            return newFileTask;
        }

        Task<StreamData> IStreamManager.Get(string id)
        {
            if (Get == null)
            {
                throw new NotImplementedException("Provide an implementation in GenericStreamManager.GetFile method.");
            }

            var fileDataTask = Task.Run(() => Get(id));

            return fileDataTask;
        }

        #endregion
    }
}